<?php
namespace app\spadmin\controller\trade;
use app\common\controller\Backend;
/**
 * 交易记录
 */
class Trade extends Backend{

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('RecordTrade');

    }

    public function index(){
        $this->request->filter(['strip_tags', 'trim']);
        if($this->request->isAjax()){
            $data = $this->request->param();
            if (isset($data['keywords']) && $data['keywords']) {
                $search = model('Account')->where('account_phone|account_card',$data['keywords'])->value('id');
                if($search>0){
                    $map['user_id'] = $search;
                }
            }
            $map['status'] =1;
            $list =   $this->model->where($map)->order('id desc')->paginate($data['limit'])->toArray();
            foreach ($list['data'] as $key => &$value) {
                if($value['account_id']){
                    $value['user'] = model('Account')->where('id',$value['account_id'])->value('account_phone');
                    $value['card'] = model('Account')->where('id',$value['account_id'])->value('account_card');
                }else{
                    $value['user'] ='';
                    $value['card'] ='';
                }                
            }
            $trade = $this->model->where($map)->where('type','recharge')->where('is_extra',0)->sum('amount');
            $account = $this->model->where($map)->where('type','expense')->where('is_extra',0)->sum('amount');
            $trade_extra = $this->model->where($map)->where('type','recharge')->where('is_extra',1)->sum('amount');
            $account_extra = $this->model->where($map)->where('type','expense')->where('is_extra',1)->sum('amount');
            return $this->result($list,0,'实际充值：<span class="layui-badge layui-bg-blue"> ¥'.number_format($trade,2).'</span>赠送充值：<span class="layui-badge layui-bg-blue"> ¥'.number_format($trade_extra,2).'</span> 实际消费：<span class="layui-badge layui-bg-orange"> ¥'.number_format($account,2).'</span>赠送消费：<span class="layui-badge layui-bg-blue"> ¥'.number_format($account_extra,2).'</span>余额：<span class="layui-badge layui-bg-green"> ¥'.number_format($trade-$account,2).'</span>');
        }
        
        return $this->view->fetch();
    }
}